package cn.afactory.tygz.sns;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lidroid.xutils.util.LogUtils;

import cn.afactory.tygz.R;
import cn.afactory.tygz.TianYaGongZaiApplication;
import cn.afactory.tygz.crash.UncaughtExceptionHandler;
import cn.afactory.tygz.sns.lsn.OnSnsResponseListener;
import cn.afactory.tygz.sns.weixin.WeiXinController;
import cn.afactory.tygz.ui.base.utils.LocalUtils;
import cn.afactory.tygz.ui.base.utils.SPUtils;

public class SnsShareActivity extends Activity {
	private String dlgTilte = null;
	private String dlgMessage = null;
	private String shareTitle = null;
	private String shareContent = null;
	private String shareImageUrl = null;
	private String shareImageLocal = null;
	private String shareWebPage = null;
	private String shareAction = null;
	private Bitmap thumb = null;
	private String shareAppendAction = null;

	private LinearLayout share_dlg_item_wechat;
//	private LinearLayout share_dlg_item_sms;
	private LinearLayout share_dlg_item_wxcircle;
	private LinearLayout share_dlg_item_qq;
	private Button share_dlg_btn_cancel;
//	private LinearLayout share_dlg_item_sina;
	private TextView mTitleView;
	
	private SNSController mSnsController;

	private final static int MESSAGE_TOAST = 1;
	private Handler myHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MESSAGE_TOAST:
				LocalUtils.showToast(SnsShareActivity.this, (String) msg.obj);
				break;
			}
		}
		
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler(
				this));
		((TianYaGongZaiApplication) this.getApplicationContext()).addActivity(this);
		
		setContentView(R.layout.sns_share_dialog);
		
		dlgTilte = getIntent().getStringExtra(SPUtils.EXTRA_DLG_TITLE);
		dlgMessage = getIntent().getStringExtra(SPUtils.EXTRA_DLG_MESSAGE);
		shareTitle = getIntent().getStringExtra(SPUtils.EXTRA_SHARE_TITLE);
		shareContent = getIntent().getStringExtra(SPUtils.EXTRA_SHARE_CONTENT);
		shareImageUrl = getIntent().getStringExtra(SPUtils.EXTRA_SHARE_IMAGE_URL);
		shareImageLocal = getIntent().getStringExtra(SPUtils.EXTRA_SHARE_IMAGE_LOCAL);
		shareWebPage = getIntent().getStringExtra(SPUtils.EXTRA_SHARE_WEB_PAGE);
		shareAction = getIntent().getStringExtra(SPUtils.EXTRA_SHARE_ACTION);
		thumb = (Bitmap)getIntent().getParcelableExtra(SPUtils.EXTRA_SHARE_WEB_PAGE_THUMB);
		
		mSnsController = SNSController.getInstance(this);
		mSnsController.initAll();
		
		initViews();
		
		initDatas();

		registReceiver();
		
	}
	
    private void registReceiver(){
    	registerReceiver(myBroadcastReceiver, new IntentFilter(SPUtils.ACTION_SHARE_RESULT));
	}
		
	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
	}
	
	@Override
	public void finish() {
		super.finish();
		((TianYaGongZaiApplication) getApplicationContext()).removeActivity(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onDestroy() {
    	if(myBroadcastReceiver != null){
			this.unregisterReceiver(myBroadcastReceiver);
		}
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	private void initViews(){
		share_dlg_item_wechat = (LinearLayout)findViewById(R.id.share_dlg_item_wechat);
//		share_dlg_item_sms = (LinearLayout)findViewById(R.id.share_dlg_item_sms);
		share_dlg_item_wxcircle = (LinearLayout)findViewById(R.id.share_dlg_item_wxcircle);
		share_dlg_item_qq = (LinearLayout)findViewById(R.id.share_dlg_item_qq);
		share_dlg_btn_cancel = (Button)findViewById(R.id.share_dlg_btn_cancel);
//		share_dlg_item_sina = (LinearLayout)findViewById(R.id.share_dlg_item_sina);
		mTitleView = (TextView)findViewById(R.id.title);
	}

	private void initDatas(){
//		if(shareType == Preference.TYPE_HANDSEL){
//			share_dlg_item_sms.setVisibility(View.VISIBLE);
//			share_dlg_item_wxcircle.setVisibility(View.GONE);
//			share_dlg_item_sina.setVisibility(View.GONE);
////			mTitleView.setVisibility(View.VISIBLE);
////			mTitleView.setText(R.string.label_share_title_handel);
//		}else{
//			share_dlg_item_sms.setVisibility(View.GONE);
//			share_dlg_item_wxcircle.setVisibility(View.VISIBLE);
//			share_dlg_item_sina.setVisibility(View.VISIBLE);
//		}
		
		share_dlg_btn_cancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
				overridePendingTransition(R.anim.push_bottom_in,R.anim.push_bottom_out);
			}
		});
		
//		if(!WeiXinController.getInstance(this).isSupported()){
//			share_dlg_item_wechat.setVisibility(View.GONE);
//			share_dlg_item_wxcircle.setVisibility(View.GONE);
//		}
		
//		if(!QQController.isSupported(this)){
//			share_dlg_item_qq.setVisibility(View.GONE);
//		}
		
		share_dlg_item_wechat.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
//				if(TimeUtil.isFastDoubleClick()){
//					return;
//				}

				if(!WeiXinController.getInstance(SnsShareActivity.this).isSupported()){
					LocalUtils.showToast(SnsShareActivity.this, R.string.weixin_not_installed);
				}else{
					mSnsController.sendText(shareTitle,
							shareContent, 
							shareImageUrl,
							shareImageLocal,
							shareWebPage,
							thumb,
							SHARE_MEDIA.WEIXIN,
							new OnSnsResponseListener() {
							@Override
							public void response(SnsErrorCode responseCode, String errorMessage) {
								responseShare(responseCode,errorMessage);
							}
						});
//					sendAppendAction();
				}
//				if(shareType == Preference.TYPE_HANDSEL){
//					setResult(Activity.RESULT_OK);
//				}
				finish();
				overridePendingTransition(R.anim.push_bottom_in, R.anim.push_bottom_out);
			}
		});
		
		share_dlg_item_wxcircle.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
//				if(TimeUtil.isFastDoubleClick()){
//					return;
//				}
				
				if(!WeiXinController.getInstance(SnsShareActivity.this).isSupported()){
					LocalUtils.showToast(SnsShareActivity.this, R.string.weixin_not_installed);
				}else{
					mSnsController.sendText(shareTitle,
							shareContent, 
							shareImageUrl,
							shareImageLocal,
							shareWebPage,
							thumb,
							SHARE_MEDIA.WEIXIN_CIRCLE,
							new OnSnsResponseListener() {
							@Override
							public void response(SnsErrorCode responseCode, String errorMessage) {
								responseShare(responseCode,errorMessage);
							}
						});
//					sendAppendAction();
				}
				finish();
				overridePendingTransition(R.anim.push_bottom_in, R.anim.push_bottom_out);
			}
		});

//		share_dlg_item_sina.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
////				if(TimeUtil.isFastDoubleClick()){
////					return;
////				}
//
//				try {
//					mWeiBoController.setResponse(
//							SnsShareActivity.this.getIntent(),
//							SnsShareActivity.this);
//
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//
//				mWeiBoController.sendTextMessage(shareTitle,
//						shareContent,
//						shareImageLocal,
//						shareImageUrl,
//						shareWebPage,
//						thumb,
//						new OnSnsResponseListener() {
//							@Override
//							public void response(SnsErrorCode responseCode,
//												 String errorMessage) {
//								responseShare(responseCode, errorMessage);
//							}
//						});
//			}
//		});
//
//		share_dlg_item_sms.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
////				if(TimeUtil.isFastDoubleClick()){
////					return;
////				}
//				mSnsController.sendText(null,
//						shareContent,
//						null,
//						null,
//						null,
//						null,
//						SHARE_MEDIA.SMS,
//						null);
////				if(shareType == Preference.TYPE_HANDSEL){
////					setResult(Activity.RESULT_OK);
////				}
//				finish();
//			}
//		});

	}
	
	private void responseShare(SnsErrorCode responseCode, String errorMessage){
		LogUtils.e("loza-SnsShareActivity-responseShare:" + responseCode + ";" + errorMessage);
		
		if(SnsErrorCode.SUCCESS == responseCode){
			Message.obtain(myHandler, MESSAGE_TOAST, getString(R.string.toast_share_success)).sendToTarget();
			finish();
		}else if(SnsErrorCode.FAILED == responseCode){
			Message.obtain(myHandler, MESSAGE_TOAST, errorMessage).sendToTarget();
		}else {
			Message.obtain(myHandler, MESSAGE_TOAST, getString(R.string.toast_share_cancel)).sendToTarget();
			finish();
		}
	}
	
	private BroadcastReceiver myBroadcastReceiver = new BroadcastReceiver(){
		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if(SPUtils.ACTION_SHARE_RESULT.endsWith(action)){
				int errorCode = intent.getIntExtra(SPUtils.EXTRA_CODE, -1);
				String errorMsg = intent.getStringExtra(SPUtils.EXTRA_MESSAGE);
				if(errorCode == SnsErrorCode.SUCCESS.ordinal()){
					finish();
				}else if(errorCode == SnsErrorCode.FAILED.ordinal()){
					
				}else if(errorCode == SnsErrorCode.CANCEL.ordinal()){
					finish();
				}
			}
		}
	};
	
	private void sendShareResultBroadcast(int errorCode,String errorMsg){
		Intent intent = new Intent(SPUtils.ACTION_SHARE_RESULT);
		intent.putExtra(SPUtils.EXTRA_CODE, errorCode);
		intent.putExtra(SPUtils.EXTRA_MESSAGE, errorMsg);
		sendBroadcast(intent);
	}

}
